package org.ntu.rtsearch.process;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ntu.rtsearch.datacollected.Constants;
import org.ntu.rtsearch.model.Tweet;

public class TweetProcess {
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = null;
		File dir = new File(Constants.BASE_PATH);
		
		for(String fileName : dir.list()) {
			if(fileName.startsWith("tweet") && !fileName.endsWith(".processed")) {
				br = new BufferedReader(new FileReader(new File(dir, fileName)));

				// process the raw json format tweets
				List<Tweet> tweets = new ArrayList<Tweet>();
				String line = null;
				int cnt = 0;
				while((line = br.readLine()) != null) {
					JSONArray jsonArr;
					try {
						jsonArr = new JSONArray(line);
					} catch (JSONException e) {
						System.out.println("line " + cnt++ + " parse error");
						continue;
					}
					
					
					for (int i = 0; i < jsonArr.length(); i++) {
						JSONObject obj = null;
						try{
							obj = jsonArr.getJSONObject(i);
						} catch (Exception e) {
							System.out.println("line " + cnt + ", tweet " + i + " parse error");
						}
						tweets.add(new Tweet(obj));
					}
					cnt++;
				}
				br.close();
				
				// quitsort by timestamp
				Collections.sort(tweets, new Comparator<Tweet>() {
					public int compare(Tweet t1, Tweet t2) {
						long diff = t1.getTimeStamp() - t2.getTimeStamp();
						return (diff > 0) ? 1 : ((diff < 0) ? -1 : 0);
					}
				});
				
				// write out to the file
				String outFileName = fileName + ".processed";
				BufferedWriter bw = new BufferedWriter(new FileWriter(new File(dir, outFileName)));
				for(Tweet tweet : tweets) {
					bw.write(+ tweet.getTimeStamp() + ":^" 
							+ tweet.getUserId() + ":^" 
							+ tweet.getText() + ":^"
							+ tweet.getRtCount() + ":^"
							+ tweet.getIsFav() + "\n");
				}
								
				bw.flush();
				bw.close();
			}
		}
		
	}
}
